import re
import time

"""
考察1
"""
def one():
  print("考察1")
  with open("pi.txt", 'r') as f:
    pi = f.read()
    # 我的生日
    birthday = int(time.mktime(time.strptime("20000101", "%Y%m%d")))
    reg = "(19[8,9][0,9]|2000)((0[1,3,5,7,8]|1[0,2])(0[1-9]|1[0-9]|2[0-9]|31)|(0[2,4,6,9]|11)(0[1-9]|1[0-9]|2[0-9]|30))"
    iter = re.finditer(reg, pi)
    array = []
    min = -1
    for index in iter:
      tmp_timestamp = time.mktime(time.strptime(index.group(), "%Y%m%d"))
      ageGap = abs(int(tmp_timestamp) - birthday)
      if -1 == min:
        min = ageGap
      elif ageGap < min:
        min = ageGap
        array.clear()
        array.append(index.group())
      elif ageGap == min:
        array.append(index.group())
      print("位置:%s,年龄:%s" %(index.start(), index.group()))
    print("离我的生日最近的是年龄:%s" %array)


"""
考察2
"""
def two():
  print("\n\n考察2")
  with open("pi.txt", 'r') as f:
    pi = f.read()
    reg = "(19([6,9])([0,9])|20(0[0,9]|1[0,9]|2[0,1]))"
    iter = re.finditer(reg, pi)
    print("以下为符合规则的年龄")
    for index in iter:
      print("位置:%s,年龄:%s" %(index.start(), index.group()))

if __name__ == '__main__':
  one()
  two()